package br.com.caelum.jdbc.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.com.caelum.jdbc.ConnectionFactory;
import br.com.caelum.jdbc.modelo.Contato;

public class ContatoDAO {

	private Connection connection;
	
	public ContatoDAO(){
		this.connection = new ConnectionFactory().getConnection();
	}
	
	public void adiciona(Contato contato){
		
		String sql = " INSERT INTO " +
					 " contatos(nome, email, endereco, dataNascimento) " +
					 " VALUES " +
					 " (?,?,?,?) ";
		
		try{
			
			PreparedStatement pstmt   = connection.prepareStatement(sql);
			
			pstmt.setString(1, contato.getNome());
			pstmt.setString(2, contato.getEmail());
			pstmt.setString(3, contato.getEnderecos());
			pstmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis())) ;
			
			pstmt.execute();
			pstmt.close();
			
		}  catch (SQLException e) {
			throw new RuntimeException();
		}
		
	}
	
}
